/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package QC_Report_Generator_v5;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;

/**
 *
 * @author dlao
 */
public class Parse {

    private Stack sk;
    private String postfix = "";
    public ArrayList<String> parseList;

    public Parse() {
        sk = new Stack();
        parseList = new ArrayList<String>();
        postfix = "";
    }

    private void doSomething(String temp, int prec) {
        String ch = temp;
        if (sk.isEmpty()) {
            sk.push(ch);
        } else {
            doSomething2(ch, prec);
        }
    }

    private void doSomething2(String ch, int prec) {
        boolean T = false;
        int prec2 = 0;;
        String temp2 = sk.peek().toString();
        if (temp2.equals("*") || temp2.equals("/")) {
            prec2 = 3;
            T = true;
        } else if (temp2.equals("+") || temp2.equals("-")) {
            prec2 = 2;
            T = true;
        } else if (temp2.equals("(")) {
            sk.push(ch);

        } else {
            prec2 = 1;
            T = true;
        }

        if (T) {
            if ((prec > prec2)) {
                sk.push(ch);
            } else {

                String temp4 = sk.pop().toString();
                postfix = postfix + temp4;
                System.out.printf("Select: %s \n", temp4);
                parseList.add(temp4);
                if (!sk.isEmpty()) {
                    doSomething2(ch, prec);
                } else {
                    sk.push(ch);
                }
            }
        }
    }

    public void toStackAndPostfix(String str2) {
        String[] str = str2.split("\\s+");
        String str3 = "";

        for (int x = 0; x < str.length; x++) {
            str3 = str3 + str[x];
        }
        System.out.println("str3: " + str3);
        StringTokenizer st = new StringTokenizer(str3.toUpperCase(), "()+-*/", true);
        while (st.hasMoreTokens()) {
            String temp = st.nextToken();
            if (temp.equals("+") || temp.equals("-")) {
                doSomething(temp, 2);

            } else if (temp.equals("*") || temp.equals("/")) {
                doSomething(temp, 3);

            } else if (temp.equals("(") || temp.equals(")")) {
                if (temp.equals(")")) {
                    while (!sk.isEmpty()) {
                        String chx = sk.pop().toString();
                        if (chx.equals("(")) {
                            break;
                        } else {
                            postfix = postfix + chx;
                            parseList.add(chx);
                        }
                    }
                    if (!sk.isEmpty()) {
                        if (sk.peek().equals("COUNT")
                                || sk.peek().equals("SUM")
                                || sk.peek().equals("DISTINCT")
                                || sk.peek().equals("%")) {
                            String str5 = sk.pop().toString();
                            parseList.add(str5);
                            postfix = postfix + str5;
                        }
                    }
                } else if (temp.equals("(")) {
                    sk.push(temp);
                }
            } else if (temp.equals("COUNT") || temp.equals("SUM") || temp.equals("DISTINCT")||temp.equals("%")) {
                sk.push(temp);
            } else {
                postfix = postfix + temp;
                parseList.add(temp);
            }
        }

        while (!sk.isEmpty()) {
            String str4 = sk.pop().toString();
            if (!str4.equals("(")) {
                postfix = postfix + str4;
                parseList.add(str4);
            }
        }
        System.out.println(parseList.toString());
        postfix = "";
    }
}
